<?php
  /**
   * Trida pro generovani leveho menu
   *
   */    
class Menu {

  
  protected $condition;  // pohlavi
  protected $menu;
  protected $gender;
  public $submenu;
  
  public function __construct ($pohlavi) {
    if($pohlavi){
      $this->condition = $pohlavi; 
    }
    else {
      $this->condition = "oboji";
    }   
  }
  
  public function generate_menu() {
 
  global $db;
  try{  
    if($this->condition === "oboji") {
     $sql_popisy = "SELECT DISTINCT n.kategorie FROM ".TBL_NAZVY. " n , ".TBL_ZBOZI." z WHERE z.id_nazvy = n.id_nazvy;" ;
     
    }
    else {
      $sql_popisy = "SELECT DISTINCT n.kategorie FROM ".TBL_NAZVY. " n , ".TBL_ZBOZI." z WHERE z.id_nazvy = n.id_nazvy AND z.pohlavi   = :1 OR z.pohlavi = 'oboji' ;";
             
    }
    
    $stmt = $db->prepare($sql_popisy);    
    $stmt->execute($this->condition);
    
    while($row = $stmt->fetch_assoc()) {      
      $this->menu[]=$row['kategorie'];          
    }  
    $_SESSION['gender'] = $this->condition;
    
  }
  
  catch(Exception $e) {
    print "Chyba v provadeni sql dotazu nebo prirayeni dat do pole";
  }
  }
  
  public function generate_submenu($typ){
  try{
      global $db;
     if($this->condition == 'oboji') {
      $sql = "SELECT DISTINCT v.nazev FROM ".TBL_ZBOZI." z LEFT JOIN ".TBL_VYROBCI." v ON v.id_vyrobce=z.id_vyrobce LEFT JOIN "
      .TBL_NAZVY." n ON n.id_nazvy=z.id_nazvy WHERE n.kategorie = :1 ;" ;      
      $stmt =  $db->prepare($sql);
      $stmt->execute($typ);
      while($row =  $stmt->fetch_assoc()) {
        $this->submenu[] = $row['nazev']; 
      }
     
     }
     else if($this->condition == 'zena'){
      $sql = "SELECT DISTINCT v.nazev FROM ".TBL_ZBOZI." z LEFT JOIN ".TBL_VYROBCI." v ON v.id_vyrobce=z.id_vyrobce LEFT JOIN "
      .TBL_NAZVY." n ON n.id_nazvy=z.id_nazvy WHERE (z.pohlavi='oboji' OR z.pohlavi='zena') AND n.kategorie = :1 ;" ;       
      $stmt =  $db->prepare($sql);
      $stmt->execute($typ);
     
      while($row =  $stmt->fetch_assoc()) {
        $this->submenu[] = $row['nazev']; 
      }
     }
     else if($this->condition == 'muz'){
      $sql = "SELECT DISTINCT v.nazev FROM ".TBL_ZBOZI." z LEFT JOIN ".TBL_VYROBCI." v ON v.id_vyrobce=z.id_vyrobce LEFT JOIN "
      .TBL_NAZVY." n ON n.id_nazvy=z.id_nazvy WHERE (z.pohlavi='oboji' OR z.pohlavi='muz') AND n.kategorie = :1 ;" ;       
      $stmt =  $db->prepare($sql);
      $stmt->execute($typ);
      
      while($row =  $stmt->fetch_assoc()) {
        $this->submenu[] = $row['nazev']; 
      }     
     }
    }
    catch(Exception $e){
    echo "Zachycena vyjimka vyvolana chybou v provadeni mysql dotazu: <br>" .$e;    
    }
    
  }
  
  
  public function assign() {
  global $smarty,$poh;
  
    $smarty->assign('submenu',$this->submenu);
    $smarty->assign('menu',$this->condition);
    $smarty->assign("typ",$this->menu);
    $smarty->assign("poh", $poh);
  }

  
}
?>
